package com.example.milktea.controller;

import com.example.milktea.entity.Order;
import com.example.milktea.entity.Product;
import com.example.milktea.service.ProductService;
import com.example.milktea.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @RequestMapping("/getProAll")
    @ResponseBody
    public List<Map<String, Object>> getProAll(){
        List<Map<String, Object>> product = this.productService.getProAll();
        return product;
    }

    @RequestMapping("/getProductById")
    @ResponseBody
    public Product getProductById(HttpServletRequest request){
        Product product = new Product();
        String product_id=request.getParameter("product_id");
        Integer id = Integer.valueOf(product_id);
        product = this.productService.getProductById(id);
        return product;
    }

    @RequestMapping("/modifyPrice")
    @ResponseBody
    public void modifyPrice(HttpServletRequest request){
        String product_id=request.getParameter("product_id");
        Integer id = Integer.valueOf(product_id);
        Integer newPrice = Integer.valueOf(request.getParameter("newPrice"));
        this.productService.modifyPrice(product_id,newPrice);
    }

//    @RequestMapping("/onoffSale")
//    @ResponseBody
//    public void onoffSale(HttpServletRequest request){
//        String product_id=request.getParameter("product_id");
//        this.productService.onoffSale(product_id);
//    }

    @RequestMapping("/onSale")
    @ResponseBody
    public void onSale(HttpServletRequest request){
        String product_id=request.getParameter("product_id");
        this.productService.onSale(product_id);
    }

    @RequestMapping("/offSale")
    @ResponseBody
    public void onoffSale(HttpServletRequest request){
        String product_id=request.getParameter("product_id");
        this.productService.offSale(product_id);
    }

    @RequestMapping("/updateProfit")
    @ResponseBody
    public void updateProfit(HttpServletRequest request){
        String product_id=request.getParameter("product_id");
        this.productService.updateProfit(product_id);
    }

    @RequestMapping("/addProduct")
    @ResponseBody
    public Product addProduct(HttpServletRequest request){
        Product product = new Product();
        //String product_id = request.getParameter("product_id");
        String product_name = request.getParameter("product_name");
        String product_descripe = request.getParameter("product_descripe");
        String product_src = request.getParameter("product_src");
        String product_price = request.getParameter("product_price");
        String product_store = request.getParameter("product_store");
        String product_profit = request.getParameter("product_profit");
        //String product_status = request.getParameter("product_status");

        //product.setProductId(Integer.valueOf(product_id));
        product.setProductName(product_name);
        product.setProductDesc(product_descripe);
        product.setProduct_store(product_store);
        product.setProductPrice(product_price);
        product.setProductSrc(product_src);
        //product.setProduct_status(Integer.valueOf(product_status));
        product.setProduct_profit(product_profit);

        this.productService.addProduct(product);
        return product;
    }
}
